package org.mspring.mlog.web.module.admin.spider;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.mspring.mlog.api.spider.entity.PublishRule;
import org.mspring.mlog.api.spider.entity.SpiderRule;
import org.mspring.mlog.api.spider.service.PublishRuleService;
import org.mspring.mlog.api.spider.service.SpiderRuleService;
import org.mspring.mlog.core.ServiceFactory;
import org.mspring.mlog.entity.Job;
import org.mspring.mlog.entity.NewsCatalog;
import org.mspring.mlog.service.NewsCatalogService;
import org.mspring.mlog.support.log.Log;
import org.mspring.mlog.web.query.PublishRuleQueryCriterion;
import org.mspring.platform.dao.support.Page;
import org.mspring.platform.dao.support.Sort;
import org.mspring.platform.utils.SessionUtils;
import org.mspring.platform.web.freemarker.widget.stereotype.Widget;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * 
 * @author Gao Youbo
 * @since 2013年7月22日
 */
@Widget
@RequestMapping("/admin/spider/publishRule")
public class Admin_SpiderPublishRuleWidget extends Spider_AbstractWidget {
    @Autowired
    private PublishRuleService publishRuleService;

    @SuppressWarnings("rawtypes")
    @RequestMapping("/list")
    @Log
    public String list(@ModelAttribute Page<PublishRule> publishRulePage, @ModelAttribute PublishRule publishRule, @RequestParam Map queryParams, HttpServletRequest request, HttpServletResponse response, Model model) {
        if (publishRulePage == null) {
            publishRulePage = new Page<PublishRule>();
        }
        if (publishRulePage.getSort() == null) {
            publishRulePage.setSort(new Sort("id", Sort.DESC));
        }
        publishRulePage = publishRuleService.listPage(new PublishRuleQueryCriterion(queryParams), publishRulePage);
        model.addAttribute("publishRulePage", publishRulePage);
        return "/admin/spider/publishRule/listPublishRule";
    }

    @RequestMapping("/create")
    public String create_view(@ModelAttribute PublishRule publishRule, HttpServletRequest request, HttpServletResponse response, Model model) {
        List<NewsCatalog> newsCatalogs = ServiceFactory.getBean(NewsCatalogService.class).getAll();
        List<SpiderRule> spiderRules = ServiceFactory.getBean(SpiderRuleService.class).getAll();
        model.addAttribute("newsCatalogs", newsCatalogs);
        model.addAttribute("spiderRules", spiderRules);
        model.addAttribute("execType", Job.ExecType.getExecTypeMap());
        return "/admin/spider/publishRule/createPublishRule";
    }

    @RequestMapping("/create/save")
    @Log
    public String create_save(@ModelAttribute PublishRule publishRule, HttpServletRequest request, HttpServletResponse response, Model model) {
        publishRuleService.createPublishRule(publishRule);
        return prompt(model, "系统提示", "规则创建成功", "/admin/spider/publishRule/list");
    }

    @RequestMapping("/edit")
    public String edit_view(@RequestParam(required = false) Long id, @ModelAttribute PublishRule publishRule, HttpServletRequest request, HttpServletResponse response, Model model) {
        if (id == null) {
            id = SessionUtils.getAttrAsLong(request, "Admin_SpiderPublishRuleWidget_edit_id");
        }
        if (id == null) {
            return prompt(model, "请先选择要修改的规则");
        }

        publishRule = publishRuleService.get(id);
        if (publishRule == null) {
            return prompt(model, "请先选择要修改的规则");
        }

        List<NewsCatalog> newsCatalogs = ServiceFactory.getBean(NewsCatalogService.class).getAll();
        List<SpiderRule> spiderRules = ServiceFactory.getBean(SpiderRuleService.class).getAll();

        model.addAttribute("newsCatalogs", newsCatalogs);
        model.addAttribute("spiderRules", spiderRules);
        model.addAttribute("publishRule", publishRule);
        SessionUtils.setAttr(request, "Admin_SpiderPublishRuleWidget_edit_id", id);
        model.addAttribute("execType", Job.ExecType.getExecTypeMap());
        return "/admin/spider/publishRule/editPublishRule";
    }

    @RequestMapping("/edit/save")
    @Log
    public String edit_save(@RequestParam(required = false) Long id, @ModelAttribute PublishRule publishRule, HttpServletRequest request, HttpServletResponse response, Model model) {
        publishRuleService.updatePublishRule(publishRule);
        return prompt(model, "系统提示", "规则修改成功", "/admin/spider/publishRule/list");
    }

    @RequestMapping("/delete")
    @Log
    public String delete(@RequestParam(required = false) Long id, @ModelAttribute PublishRule publishRule, HttpServletRequest request, HttpServletResponse response, Model model) {
        if (id != null) {
            publishRuleService.deletePublishRule(id);
        }
        return prompt(model, "系统提示", "规则删除成功", "/admin/spider/publishRule/list");
    }
}
